package com.yanqu.road.server.manager.tomb.dao;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.server.manager.tomb.entity.DbActivityTombInfo;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TombActivityInfoDaoImpl extends TempDataObjectDao<DbActivityTombInfo> {


    public static TombActivityInfoDaoImpl newInstance() {
        TombActivityInfoDaoImpl dao = new TombActivityInfoDaoImpl();
        return dao;
    }

    public static String updateSql = "update `t_c_activity_tomb_info` set maxFloor=?,maxExploreFloor=?,bossFloor=?,isBossFound=?,isBossDead=?,generateEventNum=?,updateTime=? where activityId=? and groupId=?";

    @Override
    protected boolean updateData(DbActivityTombInfo data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<Integer, DbParameter>();
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getMaxFloor()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getMaxExploreFloor()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getBossFloor()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getIsBossFound()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getIsBossDead()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGenerateEventNum()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));

        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));

        if (getDb().executeNoneQuery(updateSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSql = "insert into `t_c_activity_tomb_info`(activityId,groupId,maxFloor,maxExploreFloor,bossFloor,isBossFound,isBossDead,generateEventNum,createTime,updateTime) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DbActivityTombInfo data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> dbParameterMap = new HashMap<Integer, DbParameter>();
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getMaxFloor()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getMaxExploreFloor()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getBossFloor()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getIsBossFound()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getIsBossDead()));
        dbParameterMap.put(i++, new DbParameter(Types.INTEGER, data.getGenerateEventNum()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        dbParameterMap.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
        if (getDb().executeNoneQuery(insertSql, dbParameterMap) > 0) {
            return true;
        }
        return false;
    }


    @Override
    protected DbActivityTombInfo parseData(ResultSet rs) throws SQLException {
        DbActivityTombInfo data = DbActivityTombInfo.newInstance();
        data.setActivityId(rs.getInt("activityId"));
        data.setGroupId(rs.getInt("groupId"));
        data.setMaxFloor(rs.getInt("maxFloor"));
        data.setMaxExploreFloor(rs.getInt("maxExploreFloor"));
        data.setBossFloor(rs.getInt("bossFloor"));
        data.setIsBossFound(rs.getInt("isBossFound"));
        data.setIsBossDead(rs.getInt("isBossDead"));
        data.setGenerateEventNum(rs.getInt("generateEventNum"));

        data.setCreateTime(rs.getLong("createTime"));
        data.setUpdateTime(rs.getLong("updateTime"));

        return data;
    }

    public Map<Integer, DbActivityTombInfo> getData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String selectSql = "select * from t_c_activity_tomb_info where activityId = ?";
        Map<Integer, DbActivityTombInfo> resultMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DbActivityTombInfo data = parse(rs);
                resultMap.put(data.getGroupId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }

    public Map<Integer, DbActivityTombInfo> getDataMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, DbActivityTombInfo> dataMap = new ConcurrentHashMap<>();
        String selectSql = "select * from t_c_activity_tomb_info";
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            rs = call.executeQuery();
            while (rs.next()) {
                DbActivityTombInfo data = parse(rs);
                dataMap.put(data.getActivityId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public boolean saveData(DbActivityTombInfo data) {
        if (data.isUpdateOption()) {
            if (data.isNeedInsert()) {
                if (addData(data)) {
                    data.setNeedInsert(false);
                }
            } else {
                data.setUpdateTime(System.currentTimeMillis());
                update(data);
            }
        }
        return true;
    }

}
